<?php
class Managebaiviet extends CI_Model
{
    public $ID;
    public $MaDanhMuc;
    public $MaDanhMuc_Cha;
    public $TieuDe;
    public $NoiDungBaiViet_Viet;
    public $NoiDungBaiViet_Eng;
    public $HienThi;
    public $NgayVietBai;
    public $ImageLink;
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    public function LoadTatCaBaiViet()
    {
        $sql = "";
        $sql = "SELECT id, tieude, madanhmuc, madanhmuc_cha, hienthi FROM tintuc";
        $query = $this->db->query($sql);        
        return $query->result_array();
    }
    
    public function LoadBaiViet($where = null, $reject_id = null, $isDistinct = null, $limit = null, $start = null)
    {        
        if($where != null) $this->db->where($where);
        if($reject_id != null) $this->db->where('id <>', $reject_id);
        $this->db->order_by('ngayvietbai', 'desc');
        if($limit != '' && $start != '') $this->db->limit($limit, $start); 
        if($isDistinct != '') $this->db->distinct();           
        $query = $this->db->get('tintuc');        
        return $query;
    }
    
    public function ThemBaiVietMoi()
    {
        $data = array('tieude' => $this->TieuDe, 
                      'noidungbaiviet_viet' => $this->NoiDungBaiViet_Viet,
                      'hienthi' => $this->HienThi,
                      'ngayvietbai' => $this->NgayVietBai,
                      'madanhmuc' => $this->MaDanhMuc,
                      'madanhmuc_cha' => $this->MaDanhMuc_Cha,
                      'imagelink' => $this->ImageLink);                      
       $result = $this->db->insert('tintuc', $data);
       return $result;
    }
    
    public function XoaBaiViet()
    {        
       $result =  $this->db->delete('tintuc', array('id' => $this->ID, 'madanhmuc' => $this->MaDanhMuc));
       return $result;        
    }
    
    public function UpdateBaiViet()
    {
        $data = array('noidungbaiviet_viet' => $this->NoiDungBaiViet_Viet,
                      'tieude' => $this->TieuDe,
                      'ngayvietbai' => $this->NgayVietBai,
                      'madanhmuc' => $this->MaDanhMuc,
                      'madanhmuc_cha' => $this->MaDanhMuc_Cha); 
        $this->db->where('id', $this->ID);
        $result = $this->db->update('tintuc', $data);
        return $result;
    }
    
    public function UpdateHienThiBaiViet()
    {
        $condition = array('id' => $this->ID, 'madanhmuc' => $this->MaDanhMuc, 'madanhmuc_cha' => $this->MaDanhMuc_Cha);
        $data = array('hienthi' => $this->HienThi);
        $this->db->where($condition);        
        $result = $this->db->update('tintuc', $data);
        return $result;
    }
}
?>